﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using pc_system.Model;

namespace pc_system.View
{
    public partial class ClientInfo : Form
    {
        dbEntities db;
        vClientInfo vcif;
        float bilv = 1.0F;  //兑换的比率
        public ClientInfo(vClientInfo srcvcif)
        {
            InitializeComponent();
            ChangeComponent();
            vcif = srcvcif;

            chongzhi_button.Click +=chongzhi_fun;
            chongzhi_value.KeyPress += yanzheng_textbox;
            duihuan_value.KeyPress += yanzheng_textbox;
            duihuan_button.Click += duihuan_fun;
        }

        #region 快捷键
        //重写处理命令键方法以监听快捷键
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {

            if (keyData == Keys.Escape)
            {
                //System.Windows.Forms.SendKeys.Send("{F5}");
                this.Close();
                return true;
            }
            if (keyData == Keys.Enter)
            {
                //判断当前是做充值还是兑换积分操作
                if (chongzhi_value.Focused)
                {
                    this.chongzhi_button.PerformClick();
                }
                else
                {
                    this.duihuan_button.PerformClick();
                }
                return base.ProcessCmdKey(ref msg, keyData);
            }


            //继续原来base.ProcessCmdKey中的处理
            return base.ProcessCmdKey(ref msg, keyData);
        }


        #endregion


        #region  展示用户信息

        private void userinfo()
        {
            userinfo_id.Text = vcif.client_id.ToString();
            userinfo_credit.Text = vcif.client_credit.ToString();
            userinfo_discount.Text = vcif.client_discount.ToString();
            userinfo_money.Text = vcif.client_money.ToString();
            userinfo_name.Text = vcif.client_name;
            userinfo_phone.Text = vcif.client_phone;
            userinfo_remark.Text = vcif.client_remark; 
            return;
        }
       
        private void ClientInfo_Load(object sender, EventArgs e)
        {
            
            userinfo();
            chongzhi_value.Focus();

        }
        #endregion


        #region 充值与兑换积分

        private void chongzhi_fun(object sender, EventArgs e)
        {
            db = new dbEntities(); 
            var updat = db.clients.First(c => c.client_id == vcif.client_id);
            if (chongzhi_value.Text == "")
            {
                MessageBox.Show("请输入充值金额","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                chongzhi_value.Focus();
                return;
            }
            
            //确认是否充值
            if (MessageBox.Show("本次充值" + chongzhi_value.Text + "元", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.Cancel)
            {
                chongzhi_value.Focus();
                return;
            }

            updat.client_money = float.Parse(chongzhi_value.Text) + vcif.client_money;

            if (db.SaveChanges() <= 0)
            {
                MessageBox.Show("未能完成充值！","出错了",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            MessageBox.Show("充值成功！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            chongzhi_value.Focus();
            
            this.Close();

        }

        private void duihuan_fun(object sender, EventArgs e)
        {
            db = new dbEntities();
            var updat = db.clients.First(c => c.client_id == vcif.client_id);

            if (duihuan_value.Text == "")
            {
                MessageBox.Show("请输入兑换积分","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                duihuan_value.Focus();
                return;
            }
            
            if (int.Parse(duihuan_value.Text) > updat.client_credit)
            {
                MessageBox.Show("积分不足!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                duihuan_value.Focus();
                return;
            }
            //确认是否兑换积分
            if (MessageBox.Show("本次兑换" + duihuan_value.Text + "积分", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.Cancel)
            {
                duihuan_value.Focus();
                return;
            }

            updat.client_money = float.Parse(duihuan_value.Text) / bilv + vcif.client_money;
            updat.client_credit -= int.Parse(duihuan_value.Text);

            if (db.SaveChanges() <= 0)
            {
                MessageBox.Show("未能完成兑换！", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            MessageBox.Show("兑换成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            duihuan_value.Focus();
            db.SaveChanges();
            this.Close();
        
        }

        #endregion

        private void chongzhi_value_MouseClick(object sender, MouseEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            tb.SelectAll();
        }

        #region 验证文本框只能输入正数和小数点

        private void yanzheng_textbox(object sender,KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 58) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != (char)('.'))
            {
                MessageBox.Show("请检查输入是否有误","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                e.Handled = true;
            }
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf('.') != -1)
            {
                MessageBox.Show("只能有一个小数点", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Handled = true;
            }
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {
                MessageBox.Show("第一位不能是小数点", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Handled = true;
            }
            if (e.KeyChar != (char)('.') && ((TextBox)sender).Text == "0")
            {
                MessageBox.Show("输入不规范，0后面必须是小数点", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #endregion

        private void ClientInfo_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if (e.KeyChar == (char)Keys.Enter)
            {
                tb.SelectAll();
            }
        }

    }
}
